'''
雪球网
网址：https://xueqiu.com/today


'''

# 第一次的cookie 请求
import requests
import re
import execjs

cookies = {
    'Hm_lpvt_1db88642e346389874251b5a1eded6e3': '1728178967',
    '.thumbcache_f24b8bbe5a5934237bbc0eda20c1b6e7': 'v19aZ1yRRjYF9JE1nNh70nFQ3xn3EYoQYiiqTwUGRdRBeYe88mUOBL7/lGpFK/QyzJS8dHgOaSPnfdOj+WRkyg%3D%3D',
    'ssxmod_itna': 'QqjOY5YKiIxf21Dz6DUDGEUmo0=dDClDNYYSbDAhM8=xGXe5oDZDiqAPGhDCb4NPAdWUj02PYXttGww4XDrYcLtGFl+Gq7Hz=CqGLDmKDyKjoxxoD4+5GwD0eG+DD4DWDmn7DnxAQDjxDdupHs/a5Dbxi3uxiaDGeDe=HkDY5DhxDC9eKDwx0C91xDB1rEHCDD4ET3d4O7WR5vS=Dmom0CD7vvDlpqMz2C7RLXHYYmn8k+XnKDX2QDvg5sqtKva75hVpYPT603zx+eteuDoib3HW03Eji5tnDx5Bi5CAb5Yp+qEmD5bB+eHDDi1GoB4xD===',
    'ssxmod_itna2': 'QqjOY5YKiIxf21Dz6DUDGEUmo0=dDClDNYYSbDAhM3PA=ymdeD/bVDFhDHUFKy2DCqSiMHAhdQUdr+mnFChyuP50o1GReFxnbfv4gSC0D8bg3QrxAHgc2hrC+2pS+gEt8rktbRipI+aqmD=rAT5NowWeYAFM0aoqERzmoyD7O5H=Yv5NoKYC/aFME+CGt3HYSP1OSGC+fnfCg3fs=Mip2cwnbBvCUGvoBS6AUpC4IMNAgE64h307PL6DNYp6ay18+u52oaT8cIsiTcc9P97eS=L5e5+adQ1wEWFngUbg0BTeCF54PPYbu3pf8uR3rhOQ2zM2Gb+WHw1L38FTkQGiDTkG0tk4=+fH=2b=+bqTez8xg0dtrPtAY+hxUhHm4od7x0QaDdxG0O8oP+Sd4MDHDvnAqN3iq2t5Ade7Qe0Yy0WnfEglaM7=KaQ+fEq0vnfQd75KZxyhonnKVfhHjbPTtdiWPrEtqtsattiWzco8bi0278ROPrOsmYIp73O8nEQt2kOe34gD/Q3iQPG4bqxt8baSCNwwAnDWnfYG+EiO7WdPn7Ln2Tq7RhYnx2NEfOBGe6cKG8U4tPQgW82oICYjd2B7xzAuhT8e0FgkIGDejnaY8p9GDDqe/me60PD7QNe4wYP5DiCDWyBZNKzGrzOiAseh1nxx9hdjHvB/6PVeDGcDG7HiDD==',
}

headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    # 'Cookie': 'Hm_lpvt_1db88642e346389874251b5a1eded6e3=1728178967; .thumbcache_f24b8bbe5a5934237bbc0eda20c1b6e7=v19aZ1yRRjYF9JE1nNh70nFQ3xn3EYoQYiiqTwUGRdRBeYe88mUOBL7/lGpFK/QyzJS8dHgOaSPnfdOj+WRkyg%3D%3D; ssxmod_itna=QqjOY5YKiIxf21Dz6DUDGEUmo0=dDClDNYYSbDAhM8=xGXe5oDZDiqAPGhDCb4NPAdWUj02PYXttGww4XDrYcLtGFl+Gq7Hz=CqGLDmKDyKjoxxoD4+5GwD0eG+DD4DWDmn7DnxAQDjxDdupHs/a5Dbxi3uxiaDGeDe=HkDY5DhxDC9eKDwx0C91xDB1rEHCDD4ET3d4O7WR5vS=Dmom0CD7vvDlpqMz2C7RLXHYYmn8k+XnKDX2QDvg5sqtKva75hVpYPT603zx+eteuDoib3HW03Eji5tnDx5Bi5CAb5Yp+qEmD5bB+eHDDi1GoB4xD===; ssxmod_itna2=QqjOY5YKiIxf21Dz6DUDGEUmo0=dDClDNYYSbDAhM3PA=ymdeD/bVDFhDHUFKy2DCqSiMHAhdQUdr+mnFChyuP50o1GReFxnbfv4gSC0D8bg3QrxAHgc2hrC+2pS+gEt8rktbRipI+aqmD=rAT5NowWeYAFM0aoqERzmoyD7O5H=Yv5NoKYC/aFME+CGt3HYSP1OSGC+fnfCg3fs=Mip2cwnbBvCUGvoBS6AUpC4IMNAgE64h307PL6DNYp6ay18+u52oaT8cIsiTcc9P97eS=L5e5+adQ1wEWFngUbg0BTeCF54PPYbu3pf8uR3rhOQ2zM2Gb+WHw1L38FTkQGiDTkG0tk4=+fH=2b=+bqTez8xg0dtrPtAY+hxUhHm4od7x0QaDdxG0O8oP+Sd4MDHDvnAqN3iq2t5Ade7Qe0Yy0WnfEglaM7=KaQ+fEq0vnfQd75KZxyhonnKVfhHjbPTtdiWPrEtqtsattiWzco8bi0278ROPrOsmYIp73O8nEQt2kOe34gD/Q3iQPG4bqxt8baSCNwwAnDWnfYG+EiO7WdPn7Ln2Tq7RhYnx2NEfOBGe6cKG8U4tPQgW82oICYjd2B7xzAuhT8e0FgkIGDejnaY8p9GDDqe/me60PD7QNe4wYP5DiCDWyBZNKzGrzOiAseh1nxx9hdjHvB/6PVeDGcDG7HiDD==',
    'Pragma': 'no-cache',
    'Referer': 'https://xueqiu.com/today',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Google Chrome";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

response = requests.get('https://xueqiu.com/today', cookies=cookies, headers=headers)
## response.cookies返回的对象转字典
cookieObj = response.cookies.get_dict()
# print(cookieObj)
print('第一次cookie请求返回acw_tc：', cookieObj['acw_tc'])

# 调试第一次cookie请求返回的html页面，用于观察；
# with open('xueqiucookie1.html', 'w', encoding='utf-8') as fp:
#     fp.write(response.text)
# print('第一次cookie请求页面保存')

## 第一次cookie请求html页面中的 “var arg1=”值;
reSub = re.sub('\s', '', response.text) # 将网页中的匹配特殊字符，如空白，空格，tab等 替换为空；
arg1 = re.findall("vararg1='(.*?)';", reSub)[0] # 使用正则匹配arg1的值,取第一个
# print(arg1)

# 调用js获取 acw_sc_v2的值
# 编译JavaScript文件
with open('雪球网_今日话题.js', 'r', encoding='utf-8') as f:
    js_content = f.read()
ctx = execjs.compile(js_content)

## 调用JavaScript中的func函数
acw_sc_v2 = ctx.call('func', arg1)
print('acw_sc_v2：', acw_sc_v2)

cookieObj['acw_tc'] = arg1
cookieObj['acw_sc__v2'] = acw_sc_v2
response = requests.get('https://xueqiu.com/today', cookies=cookieObj, headers=headers)
print('第二次cookie请求：', response.text)